package com.example.demo.controller;

import com.alibaba.fastjson.JSONObject;
import com.example.demo.Utils.JSONUtil;
import com.example.demo.Utils.StringUtil;
import com.example.demo.service.ElasticSearchService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RestController;

import java.io.IOException;

/**
 * Created by wangyg6 on 2020/8/4 11:09.
 */
@RestController
@Api(tags = {"测试用"})
public class Test {
	@Autowired
	ElasticSearchService service;
	@ApiOperation(value="/log/{keyword}")
	@GetMapping("/log/{keyword}")
	public String getLogDetail(@PathVariable("keyword") String keyword) {
		String type = "olId";
		if (!StringUtil.isNumeric(keyword)) {
			type = "logId";
		}
		try {
			String result = service.search(keyword, type);
			JSONObject jsonObject = JSONUtil.parseObject(result);
			JSONObject hitsObject = jsonObject.getJSONObject("hits");
			if (hitsObject.getInteger("total") >= 1) {
				JSONObject logObj = ((JSONObject) hitsObject.getJSONArray("hits").get(0)).getJSONObject("_source")
						.getJSONObject("logObj");
				return logObj.getString("output");
			} else {
				return "hits total 0";
			}
		} catch (IOException e) {
			return e.getMessage();
		}

	}
}
